//
//  SISSTabBar.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/13.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSTabBar.h"

#define tabrbarItemNumbers 5
@implementation SISSTabBar {
    
    UIButton *_selectedBarButton;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self addLine];
        [self addBarButtons];
    }
    return self;
}

- (void)addLine {
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1/MAIN_SCALE)];
    line.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:line];
}

- (void)addBarButtons {
    
    CGFloat buttonW = self.frame.size.width / 5;
    NSArray *imageNames = @[@"tab_home", @"tab_management", @"", @"tab_news", @"tab_mine"];
    UIImage *bgImage = [PublicUtil imageWithColor:[UIColor clearColor] size:CGSizeMake(buttonW, 47)];
    // 循环创建5个按钮
    for (int i = 0; i < 5; i++) {
        
        UIButton *button = [[UIButton alloc] init];
        
        NSString *selImageName = [NSString stringWithFormat:@"%@_sel",imageNames[i]];
        
        // 设置图片
        [button setImage:[UIImage imageNamed:imageNames[i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:selImageName] forState:UIControlStateSelected];
        [button setBackgroundImage:bgImage forState:UIControlStateNormal];
        [button setBackgroundImage:bgImage forState:UIControlStateSelected];
        button.tag = i;
        [button sizeToFit];
        if (i != 2) {
            [self addSubview:button];
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self);
                make.centerX.equalTo(self).offset((i - 2) * buttonW);
            }];
        }
        
        button.imageView.contentMode = UIViewContentModeScaleAspectFit;
        
       
        // 设置第一个为选中状态
        if (i == 0) {
           
            [self buttonClick:button];
        }
    }
    
}

- (void)buttonClick:(UIButton *)button {
    
    if (button.tag != 2) {
        [self.delegate changeNav:_selectedBarButton.tag to:button.tag];
        _selectedBarButton.selected = NO;
        button.selected = YES;
        _selectedBarButton = button;
    }
}

- (void)showBadgeOnMessage {
    
    NSInteger index = 3;
    
    [self removeBadgeOnitemIndex:index];
    
    UIButton * badgeView = [[UIButton alloc]init];
    badgeView.tag = 888 + index;
    badgeView.layer.cornerRadius = 6;
    badgeView.titleLabel.font = [UIFont systemFontOfSize:12];
    
    NSString * stringX = [NSString stringWithFormat:@"%zd",self.badge];
    
    [badgeView setTitle:stringX forState:UIControlStateNormal];
    
    badgeView.backgroundColor = [UIColor redColor];
    
    CGRect tabFram = self.frame;
    
    float percentX = (index+0.55)/tabrbarItemNumbers;
    
    CGFloat x = ceil(percentX * tabFram.size.width);
    
    CGFloat y = ceil(0.2 * tabFram.size.height);
    
    badgeView.frame = CGRectMake(x, y, 12, 12);
    
    [self addSubview:badgeView];
    
//    [UIView animateWithDuration:0.15 animations:^{
//        
//        badgeView.transform = CGAffineTransformScale(badgeView.transform, 1.5, 1.5);
//        
//    } completion:^(BOOL finished) {
//        
//        
//        [UIView animateWithDuration:0.15 animations:^{
//            
//            badgeView.transform = CGAffineTransformScale(badgeView.transform, 0.666666, 0.666666);
//        }];
//        
//        
//    }];
    
}

- (void)hideBadgeOnMessage {
    
    [self removeBadgeOnitemIndex:3];
    
}

- (void)removeBadgeOnitemIndex:(NSInteger)index {
    
    
    for (UIView * subView  in self.subviews) {
        
        if (subView.tag == 888 + index) {
            
            [subView removeFromSuperview];
            
        }
    }
}

@end
